/**
 * BaiduPushSender.java 
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2014年10月19日
 */
package com.pshare.sender;

import java.util.List;

import org.apache.log4j.Logger;

import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.PushBroadcastMessageRequest;
import com.baidu.yun.channel.model.PushBroadcastMessageResponse;
import com.baidu.yun.channel.model.PushUnicastMessageRequest;
import com.baidu.yun.channel.model.PushUnicastMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;

/**
 * 百度推送
 */
public class BaiduPushSender extends PushSender {

	private static final Logger logger = Logger.getLogger(BaiduPushSender.class);
	protected static final Integer ONE_TIME_MAX = 50;
	private static ChannelKeyPair pair = null;
	private static BaiduChannelClient channelClient = null;

	public BaiduPushSender() {

	}

	public BaiduPushSender(String androidAppKey, String androidMasterSecret) {
		this.androidAppKey = androidAppKey;
		this.androidMasterSecret = androidMasterSecret;
		pair = new ChannelKeyPair(androidAppKey, androidMasterSecret);
		channelClient = new BaiduChannelClient(pair);
		channelClient.setChannelLogHandler(new YunLogHandler() {
			public void onHandle(YunLogEvent event) {
				logger.info(event.getMessage());
			}
		});
	}

	@Override
	public void doPush(Integer msgType, List<String> deviceTokens, String title, String message, 
			String objId, Integer bizType, String sound)  {
		if (msgType == 0) {
			this.broadcastMessage(msgType, message, title, bizType, sound);
		}
	}

	public void pushToChannel(Integer msgType, String deviceToken, String message, String title, Integer bizType, String sound) {
		try {
			Long channelId = Long.parseLong(deviceToken);

			// 1. 创建请求类对象
			PushUnicastMessageRequest request = new PushUnicastMessageRequest();
			request.setDeviceType(3);
			request.setChannelId(channelId);
			// request.setUserId("814432199857187466");

			request.setMessageType(msgType);
			request.setMessage("{\"title\":\"" + title + "\",\"description\":\"" + message + "\",\"biztype\":\""
					+ bizType + "\",\"sound\":\"" + sound + "}");

			// 5. 调用pushMessage接口
			PushUnicastMessageResponse response = channelClient.pushUnicastMessage(request);

			if (response.getSuccessAmount() == 1) {
				logger.info("Android推送成功 " + channelId);
			} else {
				logger.warn("Android推送失败 " + channelId);
			}
		} catch (ChannelClientException e) {
			logger.warn("百度推送客户端异常：" + e.getMessage());
		} catch (ChannelServerException e) {
			logger.error(String.format("request_id: %d, error_code: %d, error_message: %s", e.getRequestId(),
					e.getErrorCode(), e.getErrorMsg()));
		}

	}

	public void broadcastMessage(Integer msgType, String message, String title, Integer bizType, String sound) {
		try {
			// 1. 创建请求类对象
			PushBroadcastMessageRequest request = new PushBroadcastMessageRequest();
			request.setMessageType(msgType);
			request.setDeviceType(3);
			request.setMessage("{\"title\":\"" + title + "\",\"description\":\"" + message + "\",\"msgtype\":\""
					+ msgType + "\",\"sound\":\"" + sound + "}");

			// 5. 调用pushMessage接口
			PushBroadcastMessageResponse response = channelClient.pushBroadcastMessage(request);
			if (response.getSuccessAmount() == 1) {
				logger.info("Android推送成功 ");
			} else {
				logger.warn("Android推送失败 ");
			}
		} catch (ChannelClientException e) {
			logger.warn("百度推送客户端异常：" + e.getMessage());
		} catch (ChannelServerException e) {
			logger.error(String.format("request_id: %d, error_code: %d, error_message: %s", e.getRequestId(),
					e.getErrorCode(), e.getErrorMsg()));
		}
	}

}
